package com.leenmvc.core.utils.dictionary;

import com.leenmvc.core.base.BaseEntity;
import com.leenmvc.core.dao.communal.BaseDao;
import com.leenmvc.core.dao.communal.Wrapper;
import com.leenmvc.core.dao.mysql.MysqlBaseDaoImpl;
import com.leenmvc.core.security.entity.core.SysDict;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典表实现类
 */
public class DictGetImpl implements DictGet {

    /**
     * 获取持久层操作的代理类
     *
     */
    private BaseDao baseDao = MysqlBaseDaoImpl.getInstance();

    private DictGetImpl() {}

    private static DictGetImpl dictGet = null;

    /**
     * 字典表对应的类
     */
    private static final Class<?> dictClass = SysDict.class;

    public synchronized static DictGetImpl getInstance() {
        if(dictGet == null) {
            dictGet = new DictGetImpl();
        }
        return dictGet;
    }

    @Override
    public SysDict dictId(Object id) {
        return baseDao.get(dictClass, id);
    }

    @Override
    public List<SysDict> dictType(String dictType) {
        Wrapper queryWrapper = BaseEntity.getDialect(SysDict.class).getWrapperFactory().getQueryWrapper(dictClass);
        queryWrapper.eq("dictType",dictType)
                .uEq("dictValue","");
        return baseDao.query(queryWrapper);
    }

    @Override
    public Map<String, Object> getMapByType(String dictType) {
        Map<String, Object> result = new HashMap<>();
        List<SysDict> sysDicts = dictType(dictType);
        for(SysDict dict: sysDicts) {
            result.put(dict.getDictName(), dict.getDictValue());
        }
        return result;
    }

    @Override
    public List<SysDict> typeListRidPid(String dictType) {
        Wrapper queryWrapper = BaseEntity.getDialect(SysDict.class).getWrapperFactory().getQueryWrapper(dictClass);
        queryWrapper.eq("dictType", dictType).uEq("pid", 0);
        return baseDao.query(queryWrapper);
    }

    @Override
    public List<SysDict> dictPid(Object pid) {
        return baseDao.getList(dictClass, "pid", pid);
    }

    @Override
    public SysDict typeNameDict(String type, String name) {
        return baseDao.get(dictClass, "dictType", type, "dictName", name);
    }

    @Override
    public String typeNameGet(String type, String name) {
        Wrapper queryWrapper = BaseEntity.getDialect(SysDict.class).getWrapperFactory().getQueryWrapper(dictClass);
        queryWrapper.eq("dictType", type).eq("dictName", name).incField("dictValue");
        return baseDao.getOneField(queryWrapper);
    }

    @Override
    public boolean dictInsert(SysDict sysDict) {
        return baseDao.insert(sysDict);
    }

    @Override
    public boolean dictUpdate(SysDict sysDict) {
        return baseDao.update(sysDict);
    }

    @Override
    public boolean dictDelete(SysDict sysDict) {
        return baseDao.remove(sysDict);
    }

    @Override
    public int dictUpdate(Map<String, Object> params) {
        return baseDao.update(dictClass, params);
    }

    @Override
    public boolean dictDelete(Object id) {
        return baseDao.deleteById(dictClass, id);
    }
}
